package com.dhecp.project.purchase.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.purchase.domain.PurchasingSupplier;
import com.dhecp.project.purchase.service.PurchasingSupplierService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * @program: kjecp
 * @description: 线下供应商备案
 * @author: hlf
 * @create: 2022-10-28 16:48
 */
@Controller
@RequestMapping("/purchase/purchasingsupplier")
public class PurchasingSupplierController extends BaseModuleController {

    private String prefix = "purchase/purchasingsupplier";
    private String moduleTable = ModuleTable.PURCHASING_SUPPLIER;

    @Autowired
    private PurchasingSupplierService dataInfoService;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    @Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;

    /**
     * 打开查询列表
     * @param params
     * @return ModelAndView
     * @author DH-Xxj
     */
    @RequiresPermissions("purchase:purchasingsupplier:view")
    @GetMapping()
    public ModelAndView dataList(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        params.put("entity", new PurchasingSupplier()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
        Subject userSubject = ShiroUtils.getSubject();
        Integer toolBarWidth = 0;
        if (userSubject.isPermitted("purchase:purchasingsupplier:delete")){	//当前登录用户主体是否有删除的权限
            params.put("checkBox", "yes"); //生成表格gridColumns时包含复选
            toolBarWidth = toolBarWidth + 85;
        }
        if (userSubject.isPermitted("purchase:purchasingsupplier:edit")){ //当前登录用户主体是否有修改的权限
            toolBarWidth = toolBarWidth + 85;
        }
        params.put("toolBarWidth", toolBarWidth);

        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> extraButton = new HashMap<String, Object>();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        try{
            dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
            extraButton = this.checkExtraButton(params);
            if (dictionaryForFieldMap.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
                ajaxResult = AjaxResult.success("数据获取成功。");
            }
        }
        catch(Exception e){
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
        mv.addObject("ajaxResult", ajaxResult);
        mv.addObject("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));
        mv.addObject("moduleTable", moduleTable);
        mv.addObject("extraButton", TypeUtils.mapToJson(extraButton));
        mv.setViewName(prefix + "/purchasingsupplier");
        return mv;
    }

    /**
     * 查询数据
     * @param params
     * @return TableDataInfo
     * @author DH-Xxj
     */
    @GetMapping("/list")
//    @RequiresPermissions("purchase:purchasingsupplier:list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        List<PurchasingSupplier> datalist = new ArrayList<PurchasingSupplier>();
        try {
            datalist = dataInfoService.queryData(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());
        }
        return getDataTable(datalist);
    }

    /**
     * 打开选择页面
     * @param params
     * @return ModelAndView
     * @author DH-Xxj
     */
    @GetMapping("/select")
    public ModelAndView selectList(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        params.put("entity", new PurchasingSupplier()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
        params.put("toolBarWidth", 0);
        if(params.get("pagetype") !=null && params.get("pagetype").toString().equalsIgnoreCase("checkbox"))params.put("checkBox", "yes");
        else params.put("radio", "yes");
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        mv.addObject("ajaxResult", ajaxResult);
        mv.addObject("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));
        mv.addObject("prefix", prefix);
        mv.addObject("moduleTable", moduleTable);
        mv.setViewName("common/select");
        return mv;
    }

    /**
     * 打开新增页面
     * @param params
     * @return ModelAndView
     * @author DH-Xxj
     */
    @RequiresPermissions("purchase:purchasingsupplier:add")
    @GetMapping("/add")
    public ModelAndView dataAdd(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        params.put("entity", new PurchasingSupplier()); //用于获取实体属性名取代对应字典字段名，解决大小写敏感问题
        PurchasingSupplier dataInfo = new PurchasingSupplier();
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        try{
            dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
            if (dictionaryForFieldMap.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
                ajaxResult = AjaxResult.success("数据获取成功。");
            }
        }
        catch(Exception e){
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        dataInfo.setGuid(RandomValueUtils.getGUID());  //打开页面直接生成guid，用于上传关联附件
        mv.addObject("pageType","add");
        mv.addObject("dataInfo",dataInfo);
        mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
        mv.addObject("ajaxResult", ajaxResult);
        mv.setViewName(prefix + "/purchasingsupplier_add");
        return mv;
    }

    /**
     * 新增——数据提交
     * @param params
     * @return AjaxResult
     * @author DH-Xxj
     */
    @RequiresPermissions("purchase:purchasingsupplier:add")
    @Log(title = "新增合作商账户", businessType = BusinessType.INSERT)
    @PostMapping("/addpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult dataAddPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= dataInfoService.insertData(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 删除——数据提交
     * @param params
     * @return AjaxResult
     * @author DH-Xxj
     */
    @RequiresPermissions("purchase:purchasingsupplier:delete")
    @Log(title = "删除合作商账户", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult dataDelete(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= dataInfoService.deleteData(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 打开修改页面
     * @param params
     * @return ModelAndView
     * @author DH-Xxj
     */
    @RequiresPermissions("purchase:purchasingsupplier:edit")
    @GetMapping("/edit")
    public ModelAndView dataEdit(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        params.put("entity", new PurchasingSupplier()); //用于获取实体属性名取代对应字典字段名，解决大小写敏感问题
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        PurchasingSupplier dataInfo = new PurchasingSupplier();
        mv = this.gotoModelAndViewErrorPage(params, "guid", "行唯一值");
        if (mv.isEmpty()){
            try{
                dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
                if (dictionaryForFieldMap.isEmpty()){
                    ajaxResult = AjaxResult.error("未获取到字段属性。");
                }else{
                    dataInfo = dataInfoService.queryDataByGuid(params);
                    if (dataInfo == null){
                        ajaxResult = AjaxResult.error("没有查到相关数据[" + params.get("guid").toString() + "]。");
                    }else{

                        ajaxResult = AjaxResult.success("数据获取成功。");
                    }
                }
            }
            catch(Exception e){
                ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
            }
            mv.addObject("pageType","edit");
            mv.addObject("dataInfo",dataInfo);
            mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
            mv.addObject("ajaxResult", ajaxResult);
            mv.setViewName(prefix + "/purchasingsupplier_add");
        }
        return mv;
    }

    /**
     * 通过guid修改数据——数据提交
     * @param params
     * @return AjaxResult
     * @author DH-Xxj
     */
    @RequiresPermissions("purchase:purchasingsupplier:edit")
    @Log(title = "修改合作商账户", businessType = BusinessType.UPDATE)
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult dataEditPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= dataInfoService.updateByGuid(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

}